package com.guarantee.server.common.utils.encry;

import com.alibaba.fastjson2.JSON;
import org.apache.commons.codec.binary.Base64;

import javax.crypto.Cipher;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author: stx
 * @CreateTime: 2023-05-30  17:13
 * @Description: TODO
 */
public class AesUtil {

//    private static final String key = "Zhong_Guo_Tou_Biao_Bao_Han_Wang";

    // 加密
    public static String encry(Object content, String key) {
        String json = JSON.toJSONString(content);
        // IV为商户MD5密钥后16位
        String IV = key.substring(key.length() - 16);
        // RES的KEY 为商户MD5密钥的前16位
        String keys = key.substring(0, 16);
        return encryptData(json, keys, IV);
    }

    // 解密
    public static String desEncry(String content, String key) {
        // IV为商户MD5密钥后16位
        String IV = key.substring(key.length() - 16);
        // RES的KEY 为商户MD5密钥的前16位
        String keys = key.substring(0, 16);
        return decryptData(content, keys, IV);
    }

    /**
     * aes 加密
     *
     * @param data 明文
     */
    private static String encryptData(String data, String key, String IV) {
        try {
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            byte[] dataBytes = data.getBytes(StandardCharsets.UTF_8);
            int plaintextLength = dataBytes.length;
            byte[] plaintext = new byte[plaintextLength];
            System.arraycopy(dataBytes, 0, plaintext, 0, dataBytes.length);
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes());
            cipher.init(Cipher.ENCRYPT_MODE, keyspec, ivspec);
            byte[] encrypted = cipher.doFinal(plaintext);
            return new String(Base64.encodeBase64(encrypted));
        } catch (Exception e) {
            return null;
        }

    }

    /**
     * 解密
     *
     * @param data 密文
     */
    private static String decryptData(String data, String key, String IV) {
        try {
            byte[] encrypted1 = Base64.decodeBase64(data.getBytes(StandardCharsets.UTF_8));
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
            SecretKeySpec keyspec = new SecretKeySpec(key.getBytes(), "AES");
            IvParameterSpec ivspec = new IvParameterSpec(IV.getBytes());
            cipher.init(Cipher.DECRYPT_MODE, keyspec, ivspec);
            byte[] original = cipher.doFinal(encrypted1);
            return new String(original, StandardCharsets.UTF_8);
        } catch (Exception e) {
            return null;
        }
    }

    public static void main(String args[]){
        String context = "dRwxrrOh6B0BnJrYXKwG6DBjlQJ8JtuwDvGz5hlMdxo9cvOjJlIDqx+DKugVeTLG3ZhOg1+gmmEVg3qU6w9qTEadAasGnvMVGI0wfCwtIRJsooTrAM3r8Fybl98D/Adib8B8A3KaXbK+WYBdZF7wgH0/EnS8T5AfgGqSyxiN261sM3JwduMLyt/4kQxw0HQwef8YEdqqbxMqpV3o4lJrLEb6dG6Shv9vnGKjq/b9AyGVmdeMSsP7QlQJceMz2Fr9RAU+qsN29AwreKXREULsxRM8WVq6rdop/K9V3QpQ9QNgpp1ni9+9N/EFapcIPrrw47qkjTyiK2O2CEBFMARmjcYe9VW+Euzonign5kApZLMC936GYArDLz5axahDDsu8gDzhv8I+QmLWz5vUhFecrkR2aiOeJFSI7gmvqqk8aJU=";

        Map<String,Object> toubaomap = new HashMap<>();
        toubaomap.put("orderNo", "T1111111111");
        toubaomap.put("enterpriseName", "投标企业名称");
        toubaomap.put("enterpriseCode", "984521SGLOV45132");
        toubaomap.put("tendereeName", "招标人密文");
        toubaomap.put("tendereeCode", "9845sfffffff");
        toubaomap.put("projectName", "密文第一个项目");
        toubaomap.put("projectCode", "密文第一个项目编号");
        toubaomap.put("guaranteeAmount", 50000);
        toubaomap.put("paymentAmount", 500);
        toubaomap.put("redirectUrl", "");
        toubaomap.put("provinceId", "");
        toubaomap.put("cityId", "");
        toubaomap.put("payType", "");

        Map<String,Object> tuibaoMap = new HashMap<>();
        tuibaoMap.put("orderNo", "T1111111111");
        tuibaoMap.put("reason", "退了");
        tuibaoMap.put("applyForTime", "2023-05-31 18:26:00");
        tuibaoMap.put("agent", "张三");
        tuibaoMap.put("agentPhone", "13866664444");

        String zhong_guo_tou_biao_bao_han_wang = encry(tuibaoMap, "Zhong_Guo_Tou_Biao_Bao_Han_Wang");

        String zhong_guo_tou_biao_bao_han_wang1 = desEncry(zhong_guo_tou_biao_bao_han_wang, "Zhong_Guo_Tou_Biao_Bao_Han_Wang");
        System.out.println(zhong_guo_tou_biao_bao_han_wang);
    }
}
